#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/stat.h>
#include"comm.hpp"
#include<fcntl.h>
#include<unistd.h>
int main()
{
    umask(0);
    int n=mkfifo(FIFO_FILE,0666);
    if(n!=0)
    {
        perror("mkfifo fail");
        return 1;
    }
    std::cout<<"mkfifo success"<<std::endl;
    int fd=open(FIFO_FILE,O_RDONLY);
    if(fd<0)
    {
        perror("open fail");
        return 2;
    }
    std::cout<<"open fifo success"<<std::endl;
    while(1)
    {
        char buffer[1024]={0};
        int ret=read(fd,buffer,sizeof(buffer)-1);
        if(ret>0)
        {
            //读成功了：
            buffer[ret]={0};
            std::cout<<"client say:"<<buffer<<std::endl;
        }
        else if(ret==0)
        {
            std::cout<<"client quit me too"<<std::endl;
            break;
        }
        else
        {
            perror("read fail");
            break;
        }
    }
    close(fd);

    //删除管道文件：
    int ret=unlink(FIFO_FILE);
    if(ret==-1)
    {
        perror("unlink fail");
    }
    return 0;
}